package cn.t.facade.user;

import cn.t.converter.user.RoleResourceDTOConverter;
import cn.t.core.facade.FacadeImpl;
import cn.t.dto.user.ResourceInfoDTO;
import cn.t.dto.user.RoleResourceDTO;
import cn.t.model.user.RoleResourceDO;
import cn.t.service.user.RoleResourceService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门信息表服务编排
 *
 * @author l
 */
@Slf4j
@Service
@AllArgsConstructor
public class RoleResourceFacade extends FacadeImpl<RoleResourceDTO, RoleResourceDO, RoleResourceDTOConverter, RoleResourceService> {


    public void assignRights(Long roleId, List<ResourceInfoDTO> resourceInfoList) {
        List<ResourceInfoDTO> containedResourceList = resourceInfoFacade.isExistsByIdAndFullFill(resourceInfoList);
        List<ResourceInfoDTO> existedResourceList = resourceInfoFacade.findByRoleId(roleId);

        boolean isSame = ParticipantRoleFacade.isResourceSame(containedResourceList, existedResourceList);
        if (isSame) {
            return;
        }

        service.assignRights(roleId, containedResourceList);

    }

    public List<RoleResourceDTO> findByRoleId(Long roleId) {
        List<RoleResourceDO> list = service.findByRoleId(roleId);
        if (list == null || list.isEmpty()) {
            return null;
        }
        return list.stream().map(converter::model2Dto).collect(Collectors.toList());
    }


    @Autowired
    private ResourceInfoFacade resourceInfoFacade;

}





